package com.yuan.service.serviceImpl;


import com.yuan.bean.Book;
import com.yuan.bean.LibraryCard;
import com.yuan.bean.UserLogin;
import com.yuan.mapper.BookInformationMapper;
import com.yuan.mapper.LibraryCardMapper;
import com.yuan.mapper.UserInformationMapper;
import com.yuan.service.LibraryCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class LibraryCardServiceImpl implements LibraryCardService {

    @Autowired
    LibraryCardMapper libraryCardMapper;

    @Autowired
    BookInformationMapper bookInformationMapper;

    @Autowired
    UserInformationMapper userInformationMapper;

    //查询该账号还没还书的数据
    @Override
    public List<LibraryCard> selectAllByUserId(Integer userId) {
        List<LibraryCard> select = libraryCardMapper.selectAllByUserId(userId);
        return select;
    }

    //借书操作
    @Override
    public boolean borrowBook(Integer userId, Integer bookId) {
        final int i1 = userInformationMapper.selectNum(userId);
        if (i1 > 0) {//当用户的剩余借书数量大于0的时候
            int borrowNum = i1;
            borrowNum -= 1;
            final int i2 = userInformationMapper.updateNum(userId, borrowNum);
            if (i2 > 0) {//更新用户借书数量成功的时候
                libraryCardMapper.updateIn();
                final UserLogin userLogin = userInformationMapper.selectByUserId(userId);
                String userName = userLogin.getUserName();
                final Book book = bookInformationMapper.selectAllByBookId(bookId);
                String bookName = book.getBookName();
                String author = book.getAuthor();
                final int i = libraryCardMapper.borrowBook(userId, userName, bookName, author);
                if (i > 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    //还书操作
    @Override
    public boolean returnBook(Integer userId, Integer bookNo) {
        //先获取用户的借书数量，然后先自增
        final int i1 = userInformationMapper.selectNum(userId);
        int addi1 = i1;
        addi1 += 1;
        final int i2 = userInformationMapper.updateNum(userId, addi1);
        if (i2 > 0) {//当更新用户借书数量成功的时候
            final LibraryCard libraryCards = libraryCardMapper.selectAllByNo(bookNo);
            String bookName = libraryCards.getBookName();
            final int i = libraryCardMapper.returnBook(userId, bookNo, bookName);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    //动态查询所有借书记录
    @Override
    public List<LibraryCard> selectAll(String userName) {
        final List<LibraryCard> libraryCards = libraryCardMapper.selectAll(userName);
        return libraryCards;
    }

    //检查用户到期的书籍
    @Override
    public List<LibraryCard> outTime(Integer userId) {
        final List<LibraryCard> libraryCards = libraryCardMapper.outTime(userId);
        return libraryCards;
    }


}
